<template>
  <basic-container>
    <avue-crud :option="option" :search.sync="search" :table-loading="loading" :data="data" :page.sync="page"
      :permission="permissionList" :before-open="beforeOpen" v-model="form" ref="crud" @row-update="rowUpdate"
      @row-save="rowSave" @row-del="rowDel" @search-change="searchChange" @search-reset="searchReset"
      @selection-change="selectionChange" @current-change="currentChange" @size-change="sizeChange"
      @refresh-change="refreshChange" @sort-change="sortChange" @on-load="onLoad">
    </avue-crud>
  </basic-container>
</template>

<script>
import { apiDetail,apiList,apiIsOn,apiAdd,apiDel} from "@/api/rb-oem/shop/storeType";
import { mapGetters } from "vuex";
export default {
  data() {
    return {
      form: {},
      query: {},
      search: {},
      loading: false,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0
      },
      selectionList: [],
      option: {
       height: "calc(50vh - 50px)",
        calcHeight: 30,
        tip: false,
        border: true,
        index: true,
        viewBtn: false,
        selection: false,
        searchMenuSpan: 6,
        columnBtn: false,
        labelWidth:120,
        searchLabelWidth:120,
        column: [
          {
            label: "行业分类",
            prop: "firstCode",
            type: "select",
            span: 24,
            search: true,
            props: {
              label: 'firstName',
              value: 'firstCode',
            },
            cascaderItem: ['secondCode'],
            dicUrl:'/api/rb-oem/oem/mall/mall_class/list?current=-1&size=-1&type=1',
            dicFormatter:(data)=>{
              return data.data.records
            },
            rules: [{
              required: true,
              message: "请选择行业分类",
              trigger: "blur"
            }],
            hide: true,
          },
          {
            label: "行业分类",
            prop: "firstName",
            type: "input",
            //sortable: true,
            display: false,
          },
          {
            label: "行业大类",
            prop: "secondCode",
            type: "select",
            span: 24,
            search: true,
            props: {
              label: 'secondName',
              value: 'secondCode',
            },
            dicUrl:`/api/rb-oem/oem/mall/mall_class/list?current=-1&size=-1&type=2&firstCode={{firstCode}}`,
            dicFormatter:(data)=>{
              return data.data.records
            },
            rules: [{
              required: true,
              message: "请选择行业大类",
              trigger: "blur"
            }],
            hide: true,
          },
          {
            label: "行业大类",
            prop: "secondName",
            type: "input",
            //sortable: true,
            span: 24,
            rules: [{
              required: true,
              message: "请输入行业类型名称",
              trigger: "blur"
            }],
            display: false,
          },
          {
            label: "行业小类编号",
            prop: "thirdCode",
            type: "input",
            //sortable: true,
            display:false,
            hide: true,
          },
          {
            label: "行业小类",
            prop: "thirdName",
            type: "input",
            //sortable: true,
            span: 24,
            search: true,
            display:false,
          },
          {
            label: "行业小类名称",
            searchLabelWidth:120,
            prop: "name",
            type: "input",
            //sortable: true,
            span: 24,
            rules: [{
              required: true,
              message: "请输入行业小类名称`",
              trigger: "blur"
            }],
            hide:true,
          },
          
          {
            label: "优先级",
            prop: "sortNo",
            min:0,
            type:'number',
            //sortable: true,
            span: 24,
            rules: [{
              required: true,
              message: "请输入优先级",
              trigger: "blur"
            }],
          },
   
          {
            label: "创建人",
            prop: "createUserName",
            type: "input",
            //sortable: true,
            addDisplay: false,
            editDisplay: false,
            viewDisplay: true,
            overHidden: true,
          },
          {
            label: "创建时间",
            prop: "createTime",
            type: "input",
            overHidden: true,
            minWidth:140,
            //sortable: true,
            addDisplay: false,
            editDisplay: false,
            viewDisplay: false,
          },
          {
            label: "备注",
            prop: "remark",
            type: "textarea",
            //sortable: true,
            span: 24,
          },

        ]
      },
      data: []
    };
  },
  computed: {
    ...mapGetters(["permission"]),
    permissionList() {
      return {
        addBtn: this.vaildData(this.permission.storeType_add, false),
        delBtn: this.vaildData(this.permission.storeType_del, false),
        editBtn: this.vaildData(this.permission.storeType_edit, false)
      };
    },
    ids() {
      let ids = [];
      this.selectionList.forEach(ele => {
        ids.push(ele.id);
      });
      return ids.join(",");
    }
  },
  methods: {
    rowSave(row, done, loading) {
      let parm ={
        ...row,
        type:3
      }
      apiAdd(parm).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        done();
      }, error => {
        loading();
      });
    },
    rowUpdate(row, index, done, loading) {
      let parm ={
        ...row,
        type:3
      }
      apiAdd(parm).then(() => {
        this.onLoad(this.page);
        this.$message({
          type: "success",
          message: "操作成功!"
        });
        done();
      }, error => {
        loading();
      });
    },
    rowDel(row) {
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return apiDel(row.id);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
        });
    },
    handleDelete() {
      if (this.selectionList.length === 0) {
        this.$message.warning("请选择至少一条数据");
        return;
      }
      this.$confirm("确定将选择数据删除?", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(() => {
          return apiDel(this.ids);
        })
        .then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          this.$refs.crud.toggleSelection();
        });
    },
    beforeOpen(done, type) {
      if (["edit", "view"].includes(type)) {
        apiDetail({id:this.form.id}).then(res => {
          this.form = res.data.data;
        });
      }
      done();
    },
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1
      this.onLoad(this.page, params);
      done();
    },
    selectionChange(list) {
      this.selectionList = list;
    },
    selectionClear() {
      this.selectionList = [];
      this.$refs.crud.toggleSelection();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      this.loading = true;
      params.type = 3;//1行业分类 2行业大类 3行业小类 4品牌分类
      apiList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.selectionClear();
      });
    }, 
   
   
    sortChange(val) {
      if (val.order == "ascending") {
        //正序
        this.query.ascs = val.prop
        this.query.descs = ""
      } else {
        //倒序
        this.query.descs = val.prop
        this.query.ascs = ""
      }
      this.refreshChange();
    }
  }
};
</script>

<style>

</style>
